home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / mkpatch < prev    next >
Encoding:
Text File  |  1999-05-29  |  8.2 KB  |  390 lines

  1. #!/bin/sh
  2. #
  3. # mkpatch 1.1
  4. #
  5. #    Creates a more-or-less self-contained patch package.
  6. #
  7. #  Copyright (c) Alfredo K. Kojima
  8. #
  9. #
  10. #  Syntax: mkpatch old_tree new_tree patch_name
  11. #
  12. #  If a file named mkp.stuff is found in the current directory, it
  13. # will be used to extract special handling cases. Each directive must
  14. # be placed in a line of the file and the arguments must be separated
  15. # by a single space. The currently supported directives are:
  16. #
  17. # DONTDIFF <file_path>
  18. #    Do not use diff in the specified file, replacing the file instead.
  19. #    The path must exclude the top directory and the path must be
  20. #    valid from both trees.
  21. #     It will handle special cases of files that diff thinks are text 
  22. #    files, but actually are not, like XPM files created by stupid, 
  23. #    brain damaged, moron XV.
  24. #
  25. # Example mkp.stuff file:
  26. # DONTDIFF icons/somthing.xpm
  27. # DONTDIFF icons/smthingelse.xpm
  28. #
  29. #
  30. # The resulting patch pack will be relatively big (if compared to things
  31. # produced by xdelta and others), but it will be self-contained and
  32. # hopefully smaller than the whole source tree.
  33. #
  34. # You MUST run mkpatch from a directory above old_tree and new_tree.
  35. # Example:
  36. # If newTree and oldTree are located in /tmp
  37. # cd /tmp
  38. # mkpatch oldTree newTree old_to_new 
  39. #
  40. # File names cannot contain the character # or spaces
  41. #
  42.  
  43.  
  44. if test ! $# = 3 ; then
  45.     echo "$0:read the script for the syntax."
  46.     exit 1
  47. fi
  48.  
  49.  
  50.  
  51. istext() {
  52.     a=`file $1|grep text`
  53.     if test "x$a" = "x"; then
  54.         return 0
  55.     else
  56.         return 1
  57.     fi
  58. }
  59.  
  60.  
  61. put_header() {
  62.     cat << 'EOF' |sed -e "s#TMP#$TMP#" -e "s#OTREE#$OTREE#"\
  63.         -e "s#NTREE#$NTREE#" > $1
  64. #!/bin/sh
  65. #
  66. # Patch package to upgrade OTREE to NTREE
  67. #
  68. EOF
  69.     if [ $# -gt 1 ]; then
  70.         echo $2 >> $1
  71.     fi
  72.     echo "unalias rm" >> $1
  73.  
  74. }
  75.  
  76. ##########################
  77.  
  78. check_removed_files() {
  79.  
  80.     files=`grep "Only in $OTREE" $TMP/tdiff|sed -e "s#Only in $OTREE##" -e "s#: #/#" -e "s#//#/#"`
  81.  
  82.     put_header $TMP/delfiles "# Remove obsolete files"
  83. #..................
  84.     cat << EOF >> $TMP/delfiles
  85. echo "Files that are not needed anymore will be removed now."
  86. echo "Do you wish to proceed? <y/n> [y]"
  87. read foo
  88. if [ "$foo" = "n" ]; then
  89.     exit 0
  90. fi
  91.  
  92. EOF
  93. #...................
  94.  
  95.     echo "Obsoleted Files:"
  96.     for i in $files; do
  97.         echo $OTREE/$i
  98.         echo "echo \"Removing ../$i\"" >> $TMP/delfiles
  99.         if [ -d $OTREE/$i ]; then
  100.             echo "rm -rf ../$i" >> $TMP/delfiles
  101.         else
  102.             echo "rm ../$i" >> $TMP/delfiles
  103.         fi
  104.     done
  105.     
  106.     chmod +x $TMP/delfiles
  107. }
  108.  
  109.  
  110. #########################
  111.  
  112. check_new_files() {
  113.     files=`grep "Only in $NTREE" $TMP/tdiff|sed -e "s#Only in $NTREE#../#" -e "s#: #/#" -e "s#//#/#"`
  114.  
  115.     put_header $TMP/newfiles "# Copy new files"
  116.     echo "# Table of internal file names to real file names" >> $TMP/newfiles
  117.  
  118.     index=0
  119.     dindex=0
  120.     mkdir $TMP/files
  121.     echo "New Files:"
  122.     for i in $files; do
  123.         name=`basename $i`
  124.         src=`echo $i|sed -e "s#..#$NTREE#"`
  125.         file=`echo $i|sed -e "s#../##"`
  126.         echo $src
  127.         if [ -d $src ]; then
  128.             dst="dir$dindex"
  129.  
  130.             (dir=`pwd`;cd $NTREE;tar cf $dir/$TMP/files/$dst.tar $file)
  131.  
  132.             echo "$dst=\"$file\"" >> $TMP/newfiles
  133.             dindex=`expr $dindex + 1`
  134.         else
  135.             dst="file$index"
  136.             cp $src $TMP/files/$dst
  137.             echo "$dst=\"$file\"" >> $TMP/newfiles
  138.             index=`expr $index + 1`
  139.         fi
  140.     done
  141.     echo "filecount=$index" >> $TMP/newfiles
  142.     echo "dircount=$dindex" >> $TMP/newfiles
  143. #..........
  144.     cat << 'EOF' |sed -e "s#TMP#$TMP#" -e "s#OTREE#$OTREE#"\
  145.         -e "s#NTREE#$NTREE#" >> $TMP/newfiles
  146.  
  147.  
  148. #create new directories
  149. index=0
  150. mkdir tmpdir
  151. while [ $index -lt $dircount ]; do
  152.     fname="dir$index"
  153.     eval origname=\$"$fname"
  154.     echo "Recreating directory ../$origname"
  155.     (cd tmpdir; tar xf ../files/$fname.tar)
  156.     mv tmpdir/$origname ../$origname
  157.     index=`expr $index + 1`
  158. done
  159. rm -fr tmpdir
  160.  
  161. #copy files
  162. index=0
  163. while [ $index -lt $filecount ]; do
  164.     fname="file$index"
  165.     eval origname=\$"$fname"
  166.     echo "Copying file ../$origname"
  167.     cp files/$fname ../$origname
  168.     index=`expr $index + 1`
  169. done
  170.  
  171. EOF
  172. #..........
  173.     
  174.     chmod +x $TMP/newfiles
  175. }
  176.  
  177. #####################
  178.  
  179. check_binary_changes() {
  180.     files=`grep "Binary files" $TMP/bindiff|cut -d\  -f5`
  181.     files=`echo $files`
  182.     
  183.     put_header $TMP/updbinfiles "# Replace changed binary files"
  184.     echo "# Table of internal file names to real file names" >> $TMP/updbinfiles
  185.  
  186.     for i in $no_diff; do
  187.         files="$files $NTREE/$i"
  188.     done
  189.     index=0
  190.     echo "Binary files changed:"
  191.     for i in $files; do
  192.         fname="changed$index"
  193.         oname=`echo $i|sed -e "s#$NTREE##" -e "s#^/##"`
  194.         cp $i $TMP/files/$fname
  195.         echo $i
  196.         echo "$fname=\"$oname\"" >> $TMP/updbinfiles
  197.         index=`expr $index + 1`    
  198.     done
  199.     echo "filecount=$index" >> $TMP/updbinfiles
  200.  
  201. #..........
  202.     cat << 'EOF' |sed -e "s#TMP#$TMP#" -e "s#OTREE#$OTREE#"\
  203.         -e "s#NTREE#$NTREE#" >> $TMP/updbinfiles
  204.  
  205. index=0
  206. while [ $index -lt $filecount ]; do
  207.     fname="changed$index"
  208.     eval origname=\$"$fname"
  209.     echo "Replacing file ../$origname"
  210.     rm ../$origname
  211.     cp files/$fname ../$origname
  212.     index=`expr $index + 1`
  213. done
  214.  
  215. EOF
  216. #..........
  217.     chmod +x $TMP/updbinfiles
  218. }
  219.  
  220.  
  221. #####################
  222. check_text_changes() {
  223.     echo "diff'ing trees..."
  224.     diff -rq $OTREE $NTREE > $TMP/tdiff
  225.     tmp=`egrep "^Files" $TMP/tdiff|cut -d\  -f2|sed -e "s#$OTREE/##"`
  226.     
  227.     files=""
  228.     # remove excluded files
  229.     for i in $tmp; do
  230.         ok=1
  231.         for j in $no_diff; do
  232.             if test "$i" = "$j"; then
  233.             ok=0
  234.             break
  235.             fi
  236.         done
  237.         if [ $ok = 1 ]; then
  238.             files="$files $i"
  239.         fi
  240.     done
  241.     
  242.     touch $TMP/diff
  243.     touch $TMP/bindiff
  244.     # diff remaining files
  245.     for f in $files; do
  246.         diff -rc $OTREE/$f $NTREE/$f >> $TMP/tmp
  247.         foo=`egrep "^Binary" $TMP/tmp`
  248.         if test "x$foo" = "x"; then
  249.             cat $TMP/tmp >> $TMP/diff
  250.         else
  251.             cat $TMP/tmp >> $TMP/bindiff
  252.         fi
  253.         rm -fr $TMP/tmp
  254.     done
  255. }
  256.  
  257. ################# main
  258.  
  259. stripslash() {
  260.     echo $1|sed -e 's#/$##'
  261. }
  262.  
  263. OTREE=`stripslash $1`
  264. NTREE=`stripslash $2`
  265. OUTPUT=$3
  266.  
  267. TMP=$OUTPUT.patchd
  268.  
  269. rm -fr $TMP
  270. mkdir $TMP
  271.  
  272.  
  273. if [ -f mkp.stuff ]; then
  274.     echo "Using mkp.stuff file..."
  275.  
  276.     no_diff=`grep DONTDIFF mkp.stuff|cut -d\  -f2`
  277.     no_diff=`echo $no_diff`
  278. fi
  279.  
  280.  
  281.  
  282. #....................
  283. cat << 'EOF' |sed -e "s#TMP#$TMP#" -e "s#OTREE#$OTREE#" -e "s#NTREE#$NTREE#" \
  284.     > $TMP/runme
  285. #!/bin/sh
  286. #
  287. # Patch package to upgrade OTREE to NTREE
  288. #
  289. # Automatically generated by mkpatch
  290. #
  291. # Move the TMP directory to inside NTREE
  292. # and run this script.
  293. #
  294.  
  295. TARGET_TREE=OTREE
  296.  
  297. savedir=`pwd`
  298. cd ..
  299. dir=`pwd`
  300. dir=`basename $dir`
  301. cd $savedir
  302.  
  303. if test "$USER" = root; then
  304.     echo "Do not run this script as the root user"
  305.     exit 1
  306. fi
  307.  
  308. if test "$dir" != "$TARGET_TREE"; then
  309.     echo "You must move the \"TMP\" directory to inside the "
  310.     echo "\"$TARGET_TREE\" directory before running this script."
  311.     exit 1
  312. fi
  313.  
  314. echo "################################"
  315. echo "Removing Obsolete Files"
  316. echo "################################"
  317. ./delfiles
  318. echo
  319. echo "################################"
  320. echo "Copying New Files"
  321. echo "################################"
  322. ./newfiles
  323. echo
  324. echo "################################"
  325. echo "Replacing modified binary files"
  326. echo "################################"
  327. ./updbinfiles
  328. echo
  329. echo "################################"
  330. echo "Patching modified text files"
  331. echo "################################"
  332. cd ..
  333. patch -p1 -s < $savedir/diff
  334.  
  335. echo "Patching finished."
  336.  
  337. EOF
  338. #....................
  339. cat << 'EOF' |sed -e "s#TMP#$TMP#" -e "s#OTREE#$OTREE#" -e "s#NTREE#$NTREE#" \
  340.     > $TMP/README
  341.  
  342. This patch package will upgrade OTREE to NTREE.
  343. You must unpack it inside the OTREE directory or it will not work.
  344. This patch can only be applied over a clean OTREE distribution.
  345. To apply, just type (followed by a Return, of course):
  346. ./runme
  347.  
  348. EOF
  349. #....................
  350.  
  351. #....................
  352. cat << 'EOF' > $TMP/cleanup
  353. #!/bin/sh
  354. #
  355. # Remove .orig files
  356. #
  357. find .. -name \*.orig -exec rm {} \;
  358. EOF
  359. chmod +x $TMP/cleanup
  360. #....................
  361.  
  362.  
  363. # this must be the first function called 
  364. check_text_changes
  365.  
  366. check_removed_files
  367.  
  368. check_new_files
  369.  
  370. check_binary_changes
  371.  
  372. rm -f $TMP/tdiff
  373. rm -f $TMP/bindiff
  374.  
  375. chmod +x $TMP/runme
  376.  
  377. echo "Do you want to add something to the README file? <y/n> [n]"
  378. read foo
  379. if [ "$foo" = "y" ]; then
  380.     vi $TMP/README
  381. fi
  382.  
  383. rm -f $OUTPUT.tar.gz
  384.  
  385. tar czf $OUTPUT.tar.gz $TMP
  386.  
  387. rm -fr $TMP
  388.  
  389. echo "Patch pack $OUTPUT.tar.gz successfully created."
  390.